import BundleManager from "../../base/Script/core/BundleManager";
import { Utils } from "../../base/Script/utils/Utils";
import { HeroData } from "./CFCommon";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BattleRoleUi extends cc.Component {
    @property(cc.Node)
    node_role: cc.Node = null;

    @property(sp.Skeleton)
    spine_role: sp.Skeleton = null;

    @property(cc.ProgressBar)
    hpProgress: cc.ProgressBar = null;

    @property(cc.ProgressBar)
    powerProgress: cc.ProgressBar = null;

    private isOwner: boolean;
    private maxHp: number = 100;
    private data: HeroData;

    public init(isOwner:boolean, data:HeroData) : void{
        this.data = data;
        this.hpProgress.progress = 100;
        this.powerProgress.progress = 0;
        this.node_role.scaleX = isOwner ? -1 : 1;
        this.isOwner = isOwner;
        // this.spine_role.setAnimation(0, 'idle', true)
        this.loadSpine("CardFight",data.spinePath,this.spine_role);
    }

    loadSpine(bundle:string, url:string, spine:sp.Skeleton){
        BundleManager.ins().loadBundle(bundle,url,(loadedResource) => {
            spine.skeletonData = loadedResource;
            spine.setAnimation(0, 'idle', true);
        },sp.SkeletonData);
    }

    doAction(_animation:string, _loop:boolean = false, callback = null){
        this.spine_role.setAnimation(0, _animation, _loop);
        if (_animation != 'die') {
            this.spine_role.addAnimation(0, 'idle', true);
        }
    }

    onDead() {
        this.doAction('die', false);
        this.scheduleOnce(function () {
            this.node_role.active = false;
            this.node.active = false;
        }, 0.7);
    }

    updateHp(atk:number) {
        this.data.hp = this.data.hp - atk;
        let pr = Math.max(0, this.data.hp / this.maxHp);
        this.hpProgress.progress = pr;
        this.addFightWord(-atk);
    }

    getHp(){
        return this.data.hp;
    }

    updatePower(pr:number) {
        this.data.power = this.data.power + pr;
        pr = Math.max(0, this.data.power);
        this.powerProgress.progress = pr;
    }

    getPower(){
        return this.data.power;
    }

    addFightWord(value) {
        BundleManager.ins().loadBundle("CardFight","prefab/fightWord1001",(asset) => {
            let newNode = new cc.Node()
            newNode.scale = 0.5
            let newpos = cc.v3(this.node.position.x + Utils.getRandomInt(-30, 30), this.node.position.y + Utils.getRandomInt(20, 60),0)
            newNode.position = newpos
            this.node.addChild(newNode)
            let _node = cc.instantiate(asset)
            //修改数字
            if (value !== null) {
                let hurt = _node.getChildByName('hurt_label')
                if (hurt) {
                    hurt.color = new cc.Color(255,0,0);
                    let hurt_label = hurt.getComponent(cc.Label)
                    hurt_label && (hurt_label.string = value)
                }
            }
            newNode.addChild(_node)
            let _anim = _node.getComponent(cc.Animation)
            _anim.on('finished', function () {
                newNode.destroy()
            });
        },cc.Prefab);
    }

}
